package com.amoby.base.commbank.reportfile;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.amoby.base.utils.DateUtils;

/**
 * 用来解析CSV格式的文件事件信息
 * 
 */
public class EventsTXTResolver extends FileResolver {
    public ReportEvents resolver(File file) throws IOException {
        String[] lParts = null;
        String s = null;
        Map<String, String> props = new LinkedHashMap<String, String>();
        List<String> listLines = FileUtils.readLines(file);
        for (String line : listLines) {
            // lParts = line.split(":");
            // if (lParts.length > 2) {
            // throw new RuntimeException("无法识别的文件");
            // }
            lParts = StringUtils.split(line, ":", 2);
            if (lParts.length == 2) {
                props.put(lParts[0].trim(), lParts[1].trim());
            } else if (lParts.length == 1) {
                props.put(lParts[0].trim(), null);
            }
        }
        s = props.get("Transaction Reference");
        if ("N/A".equalsIgnoreCase(s) || s == null || s.length() == 0) {
            FileEvents bean = new FileEvents();
            bean.setFileName(file.getName());
            this.reportEvents = bean;
            s = props.get("Status");
            if ("RECEIVED".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_FILE_RECEIVED);
            } else if ("REJECTED".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_FILE_REJECTED);
            } else {
                throw new RuntimeException("unknown status");
            }
            bean.setClientName(props.get("Client Name"));
            bean.setReportTitle(listLines.get(0).trim());
        } else {
            TransactionGroupEvents bean = new TransactionGroupEvents();
            bean.setFileName(file.getName());
            this.reportEvents = bean;
            s = props.get("Status");
            if ("FAILED".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_TRANSACTION_FAILED);
            } else if ("AWAITING CUSTOMER APPROVAL".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_TRANSACTION_WAITING_CONFIRM);
            } else if ("POSSIBLE DUPLICATE".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_TRANSACTION_DUPLICATE);
            } else if ("DECLINED".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_TRANSACTION_DECLINED);
            } else if ("PROCESSED".equalsIgnoreCase(s)) {
                bean.setStatus(CommBizEvents.STATUS_TRANSACTION_PROCESSED);
            } else {
                throw new RuntimeException("unknown status");
            }
            bean.setClientName(props.get("Client Name"));
            bean.setReportTitle(listLines.get(0).trim());
        }
        this.reportEvents.setOriginalFilename(props.get("Filename"));
        s = props.get("Status Creation Date");
        s += " " + props.get("Status Creation Time");
        this.reportEvents.setFileDate(DateUtils.parse(s, "dd/MM/yy HH:mm"));
        return this.reportEvents;
    }
}
